var Task = _p.Model.extend({
    defaults: {
        id: null,
        title: "",
        done: false
    },

    onViewEvent: function(view, eventName, info) {
        switch(eventName) {
            case "done-change-requested":
                this.data.done = info.value;
                this.fireEvent("status-changed");
                break;
            case "title-change-requested":
                this.data.title = info.value;
                this.fireEvent("title-changed");
                break;
            case "remove-requested":
                this.destroyView();
                this.fireEvent("removed");
                break;
            default:
                return this._superCall();
                break;
        }
    },

    satelliteClasses: {
        View: {
            template: $("#template>.task"),

            render: function() {
                this._superCall();

                this.renderTitle();
                this.renderStatus();
                
                var self = this;
                $("input.done", this.el).change(function(event) {
                    self.modelCallback("done-change-requested", {
                        value: $("input.done", self.el).prop("checked")
                    });
                });

                $("div.title", this.el).click(function(event) {
                    self.el.addClass("edit");
                    $("input.title", self.el).attr("value", self.modelData.title).focus();
                });

                $("input.title", this.el).blur(function() {
                    self.el.removeClass("edit");
                }).keydown(function(event) {
                    switch(event.keyCode) {
                        case 13:
                            self.modelCallback("title-change-requested", {
                                value: $(this).attr("value")
                            });
                            self.el.removeClass("edit");
                            break;
                        case 27:
                            self.el.removeClass("edit");
                            break;
                    }
                });

                $("a.remove", this.el).click(function(event) {
                    event.preventDefault();
                    self.modelCallback("remove-requested");
                });
            },
            
            renderStatus: function() {
                !!this.modelData.done ? this.el.addClass("done") : this.el.removeClass("done");
                $("input.done", this.el).prop("checked", !!this.modelData.done);
            },

            renderTitle: function(subtask) {
                this.modelData.title ? 
                    $("div.title", this.el).text(this.modelData.title).removeClass("empty") :
                    $("div.title", this.el).text("Empty").addClass("empty");
            },

            onModelEvent: function(eventName, info) {
                switch(eventName) {
                    case "title-changed":
                        this.renderTitle();
                        break;
                    case "status-changed":
                        this.renderStatus();
                        break;
                    default:
                        return this._superCall();
                        break;
                }
            }
        }
    }
});